/*
 * MethodBody.h
 *
 *  Created on: 2011-8-2
 *      Author:
 */

#ifndef METHODBODY_H_
#define METHODBODY_H_
namespace swfstream {
class AbcFile;
class Trait;
class MethodBody: public swfstream::SwfObject {
public:
	MethodBody();
	virtual ~MethodBody();
	uint32 method;
	uint32 maxStack;
	uint32 localCount;
	uint32 initScopeDepth;
	uint32 maxScopeDepth;
	uint32 codeLength;
	uint8* code;
	uint32 exceptionCount;
	uint32 traitCount;
	std::vector<Trait*> trait;
	TiXmlElement*toTiXmlElement(AbcFile*abcFile);
	static std::string code2asm(AbcFile*abcfile, uint8*code, size_t length);
	static uint32 getVU30(uint8*buf, size_t&length) {
		uint32 r = 0;
		uint32 c;
		uint8*p = buf;
		int n = 0;
		while (1) {
			if (n == 5) {
				std::cerr << "vu30 size too long" << std::endl
						<< __FILE__<<std::dec << __LINE__<<std::endl;
				exit(2);
			}
			c = *p++;
			r += ((c & 0x7f) << (7 * n));
			//r<<=7;
			//r+=(c&0x7f);
			n++;
			//cerr<<endl<<n<<":"<<hex<<c<<" ";
			if (!(c & 0x80))
				break;
		}
		length = n;
		return r;

	}
	static int32 getS24(uint8*buf) {
		uint32 r = 0;
		uint8*p = buf;
		for (int i = 0; i < 3; i++) {
			r += (((uint32) *p) << (8 * i));
			p++;
		}
		if (r & 0x00800000) {
			r |= 0xff000000;
		}
		return *(int32*) &r;
	}
};

} /* namespace swfstream */
#endif /* METHODBODY_H_ */
